#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   75578-yum-info.sh
# Version:    1.0
# Date:       2013/12/04
# Author:     yuanhui.shi
# Email:      yuanhui.shi@cs2c.com.cn
# Summary:    02系统基本功能-07开发环境-02开发工具-06yum包管理-06通过yum查询RPM包信息
# Notes:      yum命令
# Copyright:  China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/12/04
#             -   The first one
# ----------------------------------------------------------------------

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../lib/Echo.sh

source ../../lib/XmlParse.sh


function CleanData ()
{
  mount | grep ${MOUNTDIR} && umount -f ${MOUNTDIR}

  # restore repo file
  for i in `ls ${YUMCONF}`
  do
      local m=`echo ${i%%.old*}`
      [ -f ${YUMCONF}/${m}.old ] && mv ${YUMCONF}/${m}.old ${YUMCONF}/${m}
  done

  rm -rf ${YUMCONF}/ns-server.repo

  EchoInfo "75578-yum-info.sh执行完毕"
}

trap "CleanData" EXIT INT

MOUNTDIR="/mnt"

# check /mnt is mounted
mount | grep "${MOUNTDIR}"
[ $? -eq 0 ] && umount -f ${MOUNTDIR}

EchoInfo "创建本地yum源"

mount /dev/sr0 ${MOUNTDIR} &>/dev/null
if [ $? -eq 0 ];then
    echo "PASS: moumt /dev/sr0 ${MOUNTDIR}"
else
    echo "FAIL: please check whether you have cdrom"
    exit 1
fi

# check mount success
mount | grep ${MOUNTDIR}
EchoResult "mount  | grep ${MOUNTDIR}"

# back old repo file
YUMCONF=/etc/yum.repos.d

for i in `ls ${YUMCONF}`
do
    mv ${YUMCONF}/$i ${YUMCONF}/$i.old
done

# create new repo
cat <<-EOF >${YUMCONF}/ns-server.repo 
[ns-server]
name=NeoKylin Linux Advanced Server - Os
baseurl=file:///mnt/
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-neokylin-release
enabled=1
EOF

yum clean all
EchoResult "yum clean all"
yum list &>/dev/null
EchoResult "列出所有可安装的软件包"

! yum repolist | grep "repolist: 0"
EchoResult "通过yum repolist可以查看到ns-server yum源"

yum info &>/dev/null
EchoResult "列出资源库中所有可以安装或更新的rpm包的信息"

yum info vsftpd &>/dev/null
EchoResult "列出资源库中特定的可以安装或更新以及已经安装的rpm包的信息"

yum info perl* &>/dev/null
EchoResult "列出所有以某个字符开头的软件包的信息"

yum info updates &>/dev/null
EchoResult "列出资源库中所有可以更新的rpm包的信息"

yum info installed &>/dev/null
EchoResult "列出已经安装的所有rpm包的信息"

yum search wget &>/dev/null
EchoResult "搜索匹配特定字符的rpm包的详细信息"
